package com.example.ytr.fragmenttest_104ytr.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;


import com.example.ytr.fragmenttest_104ytr.R;
import com.example.ytr.fragmenttest_104ytr.adapter.ArticleAdapter;
import com.example.ytr.fragmenttest_104ytr.bean.ArticleBean;
import com.example.ytr.fragmenttest_104ytr.iface.ArticleListener;
import com.example.ytr.fragmenttest_104ytr.model.ArticleModel;

import java.util.List;

/**
 * Created by Neusoft on 2017/4/9.
 */

public class Fragment1 extends Fragment {
    private TextView tvinfo;
    private View view = null;
    private Context context;

    public class MainActivity extends AppCompatActivity {
        private RecyclerView recyclerView;//RecyclerView控件
        private RecyclerView.LayoutManager layoutManager;//RecyclerView布局管理
        private ArticleAdapter articleAdapter; //RecyclerView的适配器
        private List<ArticleBean> list = null;//保存数据

        ArticleListener articleListener = new ArticleListener() {

            @Override
            public void onResponse(List<ArticleBean> beanlist) {
                list = beanlist;
                articleAdapter.setList(list);
                articleAdapter.notifyDataSetChanged();

            }

            @Override
            public void onFail(String msg) {
                Toast.makeText(getActivity(), "失败", Toast.LENGTH_SHORT).show();
            }


        };


        @Nullable
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            context = getActivity();
            //动态加载Fragment1的布局文件
            view = inflater.inflate(R.layout.fragment1, container, false);
            //获取布局上的控件，注意通过view获取
            tvinfo = (TextView) view.findViewById(R.id.textView8);
            tvinfo.setText("微信");
            //注意Toast第一个参数上下文
            Toast.makeText(context, "加载Fragment微信", Toast.LENGTH_SHORT).show();
            initRecyclerView();
            ArticleModel model = new ArticleModel();
            model.getResultList("article", 1, "rv1l2tidbomann2qn86nln6ur2", articleListener);


            //返回动态生成的view
            return view;
        }

        //初始化
        private void initRecyclerView() {
            recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
            layoutManager = new LinearLayoutManager(getActivity());//创建线性布局
            recyclerView.setLayoutManager(layoutManager);//设置布局管理器
            recyclerView.setHasFixedSize(true);//固定每个item高度，可以提高性能
            //list=getData();//获取数据
            articleAdapter = new ArticleAdapter(getActivity());
            articleAdapter.setList(list);//传进数据
            recyclerView.setAdapter(articleAdapter);//绑定Recyclerview和Adapter

        }


    }
}
